package com.bbs.ajax.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bbs.action.AbstractAction;
import com.bbs.model.Event;
import com.bbs.service.EventService;
import com.bbs.web.utils.JsonUtils;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author piggsoft
 * @e-mail piggsoft@163.com
 * @since 2013-4-29
 * @version 1.0
 */
@Controller
@Scope("prototype")
public class EventAction extends AbstractAction implements ModelDriven<Event>{
	
	private long startTime;
	
	private long endTime;
	
	private List<Event> events;
	
	private Event event;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6634498075804117279L;
	
	private EventService eventService;
	
	public String getEvent(){
		try{
		events = eventService.searchEvent(startTime, endTime);
		writeJson(JsonUtils.getJSONStringFromList(events));
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public String addEvent(){
		Integer id = eventService.addEvent(event);
		writeJson(JsonUtils.getBaseString("id", id));
		return null;
	}
	
	public String updateEvent(){
		eventService.updateEvent(event);
		return null;
	}
	
	public String delEvent(){
		eventService.delEvent(event);
		return null;
	}
	
	@Override
	public Event getModel() {
		return event == null ? event = new Event() : event;
	}
	
	public long getEndTime() {
		return endTime;
	}

	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}
	
	@Resource
	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

}
